package quartz;

import org.quartz.*;
import quartz.job.MyJob1;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

public class Test1 {
	public static void main(String[] args) throws SchedulerException {
		test1();
	}

	public static void test1() throws SchedulerException {

		SchedulerFactory schedFact = new org.quartz.impl.StdSchedulerFactory();

		Scheduler sched = schedFact.getScheduler();

		sched.start();

		// define the job and tie it to our HelloJob class

		JobDetail job = newJob(MyJob1.class)
				.withIdentity("myJob", "group1")
				.build();

		JobDetail job2 = newJob(MyJob1.class)
				.withIdentity("myJob2", "group2")
				.build();

		// Trigger the job to run now, and then every 40 seconds
		Trigger trigger = newTrigger()
				.withIdentity("myTrigger", "group2")
				.startNow()
				.withSchedule(simpleSchedule()
//						.withIntervalInSeconds(1)
//						.repeatForever()
				)
				.build();

		Trigger trigger2 = newTrigger()
				.withIdentity("myTrigger2", "group3")
				.startNow()
				.withSchedule(simpleSchedule()
						.withIntervalInSeconds(1)
						.repeatForever())
				.build();

		// Tell quartz to schedule the job using our trigger
		sched.scheduleJob(job, trigger);
//		sched.scheduleJob(job2, trigger); // 报错, Exception in thread "main" org.quartz.SchedulerException: Trigger does not reference given job!
//		sched.scheduleJob(job, trigger2); // 报错, job要唯一 Exception in thread "main" org.quartz.ObjectAlreadyExistsException: Unable to store Job : 'group1.myJob', because one already exists with this identification.
	}

}



